<template>
  <div class="proDialog">
    <el-dialog
      title="选择商品"
      :visible.sync="visible"
      width="800px"
      z-index="1500"
      append-to-body
      @close="toCancelBox"
    >
      <el-table
        ref="indexTable"
        :data="infoList"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        :row-key="bindRowKeys"
      >
        <el-table-column
          type="selection"
          align="center"
          :reserve-selection="true"
        >
        </el-table-column>
        <el-table-column
          prop="carouselImgOne"
          label="图片"
          align="center"
          width="100"
        >
          <template slot-scope="scope">
            <!-- <el-image
              v-if="
                scope.row.coverImgUrl != null && scope.row.coverImgUrl != ''
              "
              slot="reference"
              :src="scope.row.coverImgUrl"
              class="image-rows"
              :preview-src-list="[scope.row.carouselImgOne]"
            >
            </el-image> -->
            <div
                    style="width: 45px; height: 45px"
                    v-html="imgTransition(scope.row.coverImgUrl)"
                  ></div>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="商品名称"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="retailPriceStr"
          label="零售价(元)"
          align="center"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.priceType == 1">{{ scope.row.price }}</div>
            <div v-if="scope.row.priceType == 2">
              <div>{{ scope.row.price }}--{{ scope.row.price2 }}</div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          style="
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
          "
          background
          layout="prev, pager, next"
          :page-size="query.pagelimit"
          :total="total"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
      <!-- <pagination style="    margin-top: -10px;" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" :page-sizes="pageSizess" /> -->
      <el-button
        style="margin-top: 47px; margin-bottom: -5px; margin-left: 89%"
        class="determine"
        @click="determine"
        >确定</el-button
      >
    </el-dialog>
  </div>
</template>
<script>
import { getGoodsList } from "@/api/applet/broadcastGood";
import { listInfo } from "@/api/product/info";
export default {
  props: {
    flag: {
      type: Boolean,
    },
  },
  watch: {
    flag(val) {},
  },
  data() {
    return {
      datas: null,
      selectedDatas: [],
      pageSizess: [5, 10, 15, 20, 25, 30, 50],
      // 商品信息表格数据
      infoList: [],
      // indexTable: [],
      infoListHistory: [],
      multipleSelection: [],
      total: 0,
      query: {
        offset: 1,
        limit: 5,
        status: 2,
      },
      visible: false,
    };
  },
  created() {
    this.visible = this.flag;
    this.getList();
  },
  methods: {
    toCancelBox() {
      this.visible = false;
      this.$emit("toCloseBox", false);
    },
    handleCurrentChange: function (cpage) {
      this.query.offset = cpage;
      this.getList();
    },

    greet(selectedData) {
      this.$nextTick(() => {
        this.selectedDatas = selectedData;
        this.infoList.forEach((outerItem) => {
          this.selectedDatas.forEach((item) => {
            if (outerItem.goodsId == item.goods_id) {
              this.$refs.indexTable.toggleRowSelection(outerItem, true);
            }
          });
        });
      });
    },
    bindRowKeys(row) {
      return row.goodsId;
    },
    //转换微信图片
      imgTransition(img) {
        var imgid = Math.random(),
          frameid = 'frameimg' + imgid;
        window['img' + imgid] = '<img id="img" style="width:45px;height:45px" src=\'' + img + '?kilobug\' />' +
          '<script>window.onload = function() { parent.document.getElementById(\'' + frameid + '\').height = document.getElementById(\'img\').height+\'px\'; }<' + '/script>';
        let msg = '<iframe id="' + frameid + '" src="javascript:parent[\'img' + imgid + '\'];" frameBorder="0" scrolling="no" width="100px"></iframe>'
        return msg
      },
    handleSelectionChange(val) {
      this.datas = this.multipleSelection;
      this.multipleSelection = val;
      if (this.datas.length > 0) {
        for (let i = 0; i < this.datas.length; i++) {
          for (let k = 0; k < this.multipleSelection.length; k++) {
            if (this.datas[i].goodsId == this.multipleSelection[k].goodsId) {
              this.multipleSelection[k] = this.datas[i];
            }
          }
        }
      }
    },
    determine() {
      this.$emit("multipleSelection", this.multipleSelection);
      this.infoList.forEach((item) => {
        this.$refs.indexTable.toggleRowSelection(item, false);
      });
      this.$forceUpdate();
      this.open = false;
    },
    /** 查询商品信息列表 */
    getList() {
      getGoodsList(this.query).then((response) => {
        this.infoList = response.data.goods;
        this.greet(this.selectedDatas);
        this.total = response.data.total;
        this.loading = false;
      });
    },
  },
};
</script>
<style scoped>
.determine {
  position: relative;
  top: -14px;
  background-color: #155bd4;
  color: #fff;
}
.pagination-container {
  position: relative;
  top: 14px;
}
.pagination {
  height: 30px;
  position: relative;
  bottom: -20px;
}
</style>
